package com.yonyou.jrmob.lryapp;

import android.text.TextUtils;

import com.yonyou.uap.um.base.UMEventArgs;
import com.yonyou.uap.um.core.UMActivity;
import com.yonyou.uap.um.log.ULog;
import com.yonyou.uap.um.service.ServiceCallback;
import com.yyuap.summer.core.IUAPServiceCallback;

import org.json.JSONObject;

import java.lang.reflect.Method;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.LogUtils;

public class RTHelperA {
    public RTHelperA() {
    }

    public static void execCallBack(UMEventArgs args) {
        if(args.containkey("callback_object")) {
            Object callback = args.get("callback_object");
            if(callback instanceof ServiceCallback) {
                IUAPServiceCallback summerExposedJs1 = (IUAPServiceCallback)callback;
                String ctx1 = args.getString("status");
                if(ctx1.equals("error")) {
                    summerExposedJs1.error("code", args.getString("statuscode", ""));
                } else {
                    //args.put("callback","bb()");

                    JSONObject jsonObject = args.toJSONObject();
                    //LogUtils.json(jsonObject);

                    summerExposedJs1.execute(jsonObject);
                }

                args.remove("status");
                return;
            }
        }

//        args.put("callback","bb()");
        String callback1 = args.getString("callback", "");
        String summerExposedJs = args.getString("SummerExposedJs", "false");
        if(!TextUtils.isEmpty(callback1) && !summerExposedJs.equals("true")) {
            args.remove("SummerExposedJs");
            UMActivity ctx = args.getUMActivity();
            if(ctx == null) {
                LogUtils.e( "ctx is null");
            }

            ctx.run(callback1, args);
        }
    }

    public static void enterService(Class serviceCls, String method, UMEventArgs args) {
        ULog.logService("[start]" + serviceCls.getName() + "." + method, args);
    }

    public static void exitService(Class serviceCls, String method, UMEventArgs args) {
        ULog.logService("[end]" + serviceCls.getName() + "." + method, args);
    }

    public static String runService(String method, UMEventArgs args) {
        ULog.logService(method, args);
        int pos = method.indexOf(46);
        String cls = method.substring(0, pos);
        method = method.substring(pos + 1);

        try {
            Class e = Class.forName("com.yonyou.uap.um.runtime." + cls);
            Method m = e.getMethod(method, new Class[]{UMEventArgs.class});
            Object rs = m.invoke(e, new Object[]{args});
            if(rs != null) {
                return rs.toString();
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return "VOID";
    }
}
